﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GalleryCommon;
using System.Web.UI.HtmlControls;

namespace GalleryWeb
{
    
    public partial class AlbumView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["User"] == null)
            {
                Response.Redirect("login.aspx");
            }
            User SessionUser;
            SessionUser = (User)Session["User"];
            List < Album > albums;
            GalleryLogic.AlbumLogic.ProvideAlbums(SessionUser.UserID, out albums);
            TableAlbums.Rows.Clear();
            int count = 0;
            for (int i = 1; i < 4; i++)
            {
                HtmlTableRow row = new HtmlTableRow();
                for (int j = 1; j < 4; j++)
                {
                    
                    try
                    {
                        HtmlTableCell cell = new HtmlTableCell();
                        AlbumControlV2 albi;
                        albi = (GalleryWeb.AlbumControlV2)LoadControl("~/AlbumControlV2.ascx");
                        albi.TextPart.Text = albums[count].Name;
                        cell.Controls.Add(albi);
                        row.Cells.Add(cell);
                        long temp = albums[count].ID;
                        albi.ImagePart.Click += (a, b) => DynamicAlbumButton_Click(sender, e, temp);
                        count++;
                    }
                    catch { }
                TableAlbums.Rows.Add(row);
                }
                TableAlbums.Rows.Add(row);
            }
        }

        

        protected void DynamicAlbumButton_Click(object sender, EventArgs e, long AlbumID)
        {
            Button button = sender as Button;
            User SessionUser;
            SessionUser = (User)Session["User"];
            SessionUser.ActiveAlbumID = AlbumID;
            Session["User"] = SessionUser;
            Response.Write(AlbumID);
            Response.Redirect("MainView.aspx");
        }

        protected void ButtonAddNewAlbum_Click(object sender, EventArgs e)
        {
            User SessionUser;
            SessionUser = (User)Session["User"];
            GalleryLogic.AlbumLogic.AddNewAlbum(TextBoxNewAlbumName.Text, SessionUser.UserID);
            ClientScript.RegisterStartupScript(typeof(Page), "autoPostback", ClientScript.GetPostBackEventReference(this, String.Empty), true);
        }

    }
}